package com.example.tnot.Teacher.Homepart.InternalFragment.Frequent.Agent;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.tnot.R;
import com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Publish.ItemBean.ListCourse;
import com.example.tnot.Teacher.Homepart.InternalFragment.Frequent.Agent.Adapter.rvAgent;
import com.example.tnot.Teacher.MinePart.InternalFragment.collection.CollectInternalFragment;
import com.example.tnot.Teacher.MinePart.InternalFragment.collection.ItemBean.rv_collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class AgentInternalFragment extends Fragment {
    private static final String ARG_CONTENT = "content";
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private List<ListCourse> data;
    View view;
    TextView tv_non;
    RecyclerView rv_notManage;

    private String currentContentType = "全部";
    private String currentCourseType = "全部";

    public AgentInternalFragment() {
    }
    public static AgentInternalFragment newInstance(String content) {
        AgentInternalFragment fragment = new AgentInternalFragment();
        Bundle args = new Bundle();
        args.putString(ARG_CONTENT, content);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view =  inflater.inflate(R.layout.home_agent_internal, container, false);
        InitData();
        InitViews();
        setListener();
        rv_notManage.setLayoutManager(new LinearLayoutManager(getActivity()));

        String type = getArguments().getString(ARG_CONTENT);
        if(type !=null) {
            currentContentType = type;
        }
        updateRecyclerView();
        return view;
    }

    void updateRecyclerView() {
            // 添加view空值检查
            if (view == null) {
                return;
            }

            List<ListCourse> filteredData = filterData(currentContentType, currentCourseType);
            if (filteredData.isEmpty()){
                tv_non.setVisibility(View.VISIBLE);
                rv_notManage.setVisibility(View.GONE);
            }else {
                tv_non.setVisibility(View.GONE);
                rv_notManage.setVisibility(View.VISIBLE);
            }
            rvAgent adapter = new rvAgent(filteredData);
            rv_notManage.setAdapter(adapter);
    }
    private void InitData() {
        data = new ArrayList<>();
        data.add(new ListCourse(R.drawable.ic_homework, "数学家庭作业", "2021-05-05", "未批改","数科2301","作业","数学"));
        data.add(new ListCourse(R.drawable.ic_exam, "英语第一次月考", "2023-05-05", "未批改","数科2301","考试","英语"));
        data.add(new ListCourse(R.drawable.ic_homework, "英语家庭作业", "2024-05-05", "未批改","数科2301","作业","英语"));
        data.add(new ListCourse(R.drawable.ic_exam, "计算机第一次月考", "2020-05-05", "未批改","数科2301","考试","计算机"));
        data.add(new ListCourse(R.drawable.ic_task, "英语第一次月考", "2021-05-04", "未批改","数科2301","任务","英语"));
        data.add(new ListCourse(R.drawable.ic_task, "数学第一次月考", "2021-05-05", "未批改","数科2301","任务","数学"));
        data.add(new ListCourse(R.drawable.ic_task, "英语第一次月考", "2021-05-05", "未批改","数科2301","任务","英语"));
        // 按时间排序（假设日期格式为 yyyy-MM-dd）
        Collections.sort(data, new Comparator<ListCourse>() {
            @Override
            public int compare(ListCourse o1, ListCourse o2) {
                return o1.getIv_Time().compareTo(o2.getIv_Time());
            }
        });
    }

    private void InitViews() {
        rv_notManage = view.findViewById(R.id.rv_notManage);
        tv_non = view.findViewById(R.id.tv_non);

    }
private void setListener() {

}


    private List<ListCourse> filterData(String contentType, String courseType) {
        List<ListCourse> filteredData = new ArrayList<>();

        // 添加空值检查
        if (data == null) {
            return filteredData;
        }
        for (ListCourse item : data) {
            boolean typeMatch = contentType.equals("全部") || item.getType().equals(contentType);
            boolean courseMatch = courseType.equals("全部") || item.getCourseName().equals(courseType);

            if (typeMatch && courseMatch) {
                filteredData.add(item);
            }
        }

        return filteredData;
    }

    // 在 AgentInternalFragment 中
    public void updateFilter(String contentType,String courseType) {
        currentContentType = contentType;
        currentCourseType = courseType;
        updateRecyclerView();
    }
}